package com.venue.hjt.venueclass.ui

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.WindowManager
import com.bumptech.glide.Glide
import com.google.gson.Gson

import com.venue.hjt.venueclass.R
import com.venue.hjt.venueclass.adapter.VenueGroupAdapter
import com.venue.hjt.venueclass.adapter.VenuePersonalAdapter
import com.venue.hjt.venueclass.adapter.VenuePersonalZAdapter
import com.venue.hjt.venueclass.bean.API
import com.venue.hjt.venueclass.bean.PersonalBean
import com.venue.hjt.venueclass.bean.StudentBean
import com.venue.hjt.venueclass.utlis.Constant
import com.venue.hjt.venueclass.utlis.OkhttPost
import kotlinx.android.synthetic.main.activity_venue_group.*
import kotlinx.android.synthetic.main.activity_venue_order.*
import kotlinx.android.synthetic.main.activity_venue_personal.*
import okhttp3.Call
import java.lang.Exception
import java.util.ArrayList

class VenuePersonalActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar!!.hide()
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
        setContentView(R.layout.activity_venue_personal)
        val student_no = intent.getStringExtra("student_no")
        ll_personal_y.setOnClickListener({
            val intent = Intent(this@VenuePersonalActivity, VenueOrderActivity::class.java)
            intent.putExtra("student_no",student_no )
            startActivity(intent)
        })
        iv_personal_close1.setOnClickListener({
            i=1
        })
        ll_news.setOnClickListener({
            val intent=Intent(this@VenuePersonalActivity,ParentNewsActivity::class.java)
            intent.putExtra("student_no",student_no)
            startActivity(intent)

        })
        Thread(Runnable {
            while (i > 0) {
                handler.sendEmptyMessage(1)
                if (i <= 0) {
                    break
                }
                try {
                    Thread.sleep(1000)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
                i--
            }
            handler.sendEmptyMessage(2)
        }).start()
        OkhttPost.Okget(API.VENUESTUDENTDATA+ Constant.SNO+"/"+student_no,1,object : OkhttPost.Callback {
            override fun onError(call: Call?, e: Exception?, id: Int) {
            }

            override fun onResponse(response: String?, id: Int) {
                Log.e("ATG",response)
                val gosn= Gson()
                try {
                    val bean = gosn.fromJson(response, StudentBean::class.java)
                    if (bean.code==1){
                        Glide.with(this@VenuePersonalActivity).load(bean.data.headimg).into(iv_student_url)
                        Log.e("ATG","personal")
                        tv_student_name.text=bean.data.name
                        tv_student_class.text=bean.data.classname
                        tv_personal_score.text=bean.data.score
                        tv_personal_plus.text=bean.data.minus
                        tv_personal_minus.text=bean.data.plus
                        val data = bean.data
                        val adapter=VenuePersonalAdapter(this@VenuePersonalActivity,data.booking_list)
                        lv_personal.adapter=adapter
                        val adapter1=VenuePersonalZAdapter (this@VenuePersonalActivity,data.groups)
                        gv_personal_z.adapter=adapter1
                        gv_personal_z.setOnItemClickListener { adapterView, view, i, l ->
                            val intent=Intent(this@VenuePersonalActivity,VenueGroupActivity::class.java)
                            intent.putExtra("id",data.groups[i].id)
                            startActivity(intent)
                        }
                    }
                }catch (e:Exception ){

                }

            }
        })
    }
    internal var i = 80
    internal var handler: Handler = object : Handler() {
        override fun handleMessage(msg: android.os.Message) {
            if (msg.what == 1) {
//                tv_countdown.text = "还剩" + i + "关闭界面"
                tv_personal_countdown1.text="还剩" + i + "关闭界面"
            } else {
                this@VenuePersonalActivity.finish()
            }
        }
    }
}
